import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
 * 线程工厂演示示例
 */
public class ThreadPoolDemo3 {
    public static void main(String[] args) {
        //1.创建线程工厂
        ThreadFactory factory=new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                //一定要把任务Runnable设置给新线程
                Thread thread=new Thread(r);
                //设置线程的命名规则
                thread.setName("我的线程"+r.hashCode());
                //设置线程的优先级
                thread.setPriority( Thread.MAX_PRIORITY);
                return thread;
            }
        };
        ExecutorService service=Executors.newFixedThreadPool(5, factory);
        for (int i = 0; i <5 ; i++) {
            service.submit(()->{
                //任务
                Thread thread=Thread.currentThread();
                System.out.println("线程池开始执行了："+Thread.currentThread().getName()+"线程优先级"+thread.getPriority());
            });
        }
    }
}
